package com.ddd.mill.api.product;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ddd.mill.api.product.dto.request.GetProductPageParamDto;
import com.ddd.mill.api.product.dto.request.SaveProductByAdminDto;
import com.ddd.mill.api.product.dto.response.UpdateStatusDto;
import com.ddd.mill.query.product.ProductQueryService;
import com.ddd.mill.api.product.dto.request.AddProductDto;
import com.ddd.mill.api.product.dto.response.GetProductPageDto;
import com.ddd.mill.api.product.dto.response.ProductDetailDto;
import com.ddd.mill.application.product.ProductApplicationService;
import com.ddd.mill.infrastructure.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author liu
 * @title: ProductController
 * @projectName trading-center
 * @date 2020/11/3010:50
 */
@Api(tags = "商品管理")
@Slf4j
@RestController
public class ProductController {

    @Autowired
    ProductApplicationService productApplicationService;

    @Autowired
    ProductQueryService productQueryService;

    @ApiOperation("管理员保存商品")
    @PostMapping("/admin/save/product")
    public void saveProductByAdmin(@Valid @RequestBody SaveProductByAdminDto dto, @RequestParam Long adminId){
        productApplicationService.saveProductByAdmin(dto,adminId);
    }
    @ApiOperation("产品列表")
    @GetMapping("/admin/get/product-list")
    public IPage<GetProductPageDto> productList(GetProductPageParamDto dto,
                                                PageUtil pageUtil){
        return productQueryService.productList(dto,pageUtil.toPage());
    }

    @ApiOperation("产品详情")
    @GetMapping("/admin/get/product/{id}")
    public ProductDetailDto productDetail(@ApiParam("id") @PathVariable Long id){
        return productQueryService.productDetail(id);
    }

    @ApiOperation("复制产品")
    @PostMapping("/admin/copy/product")
    public void copyProduct(@ApiParam("id集合，逗号隔开") @RequestParam String idList,
                            @RequestParam Long adminId){
        productApplicationService.copyProduct(idList.split(","),adminId);
    }

    @ApiOperation("编辑产品")
    @PutMapping("/admin/{id}")
    public void updateProduct(@ApiParam("id") @PathVariable Long id,
                              @RequestBody @Valid AddProductDto request,
                              @RequestParam Long adminId){

    }

    @ApiOperation("产品上下架")
    @PutMapping("/status/{id}")
    public void updateStatus(@ApiParam("id") @PathVariable Long id,
                             @RequestBody @Valid UpdateStatusDto request,
                             @RequestParam Long adminId){
        productApplicationService.updateStatus(id,request,adminId);
    }

    @ApiOperation("产品删除")
    @DeleteMapping("")
    public void deleteProduct(@ApiParam("id集合，逗号隔开") @RequestParam String idList,
                              @RequestParam Long adminId){
        productApplicationService.deleteProduct(idList.split(","), adminId);
    }

}
